<?php

namespace app\admin\controller;

use app\admin\model\Area;
use app\common\controller\Backend;
use app\common\model\Config;
use app\common\service\User;
use think\Db;
/**
 * 代理申请管理
 *
 * @icon fa fa-circle-o
 */
class Proxlog extends Backend
{
    
    /**
     * Proxlog模型对象
     * @var \app\admin\model\Proxlog
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Proxlog;
        $this->view->assign("serverTypeList", $this->model->getServerTypeList());
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("isAgentList", $this->model->getIsAgentList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->with(['user'])
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->with(['user'])
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            foreach ($list as $key=>$row) {
                
                $row->getRelation('user')->visible(['username']);
                if($row['province']){
                    $list[$key]['province']=Area::get($row['province'])['name'];
                }
                if($row['city']){
                    $list[$key]['city']=Area::get($row['city'])['name'];
                }
                if($row['area']){
                $list[$key]['area']=Area::get($row['area'])['name'];
                }

                if($row['regione']){
                    $list[$key]['regione']=Area::get($row['regione'])['name'];
                }
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
    public function detail($ids){
        $this->view->assign('id',$ids);
        return $this->view->fetch();
    }
    public function up(){
        if ($this->request->isAjax()) {
            $id=$this->request->post('id');
            $status=$this->request->post('status');

            $log=$this->model->get(['id' => $id]);
            if($status==2){
                if($log['server_type']>0){
                    $dat['server_type']=0;
                    Db::name('user')->where(['id'=>$log['user_id']])->update($dat);
                }

                if($log['is_agent']>0){
                    $dat['is_agent']=0;
                    Db::name('user')->where(['id'=>$log['user_id']])->update($dat);
                }

                $this->model->upDeposit($id,2);
               ;
                $this->success('审核失败',0);
            }
            if($log['status']>0){
                $this->success('已经审核过了,不能在再次审核了',0);
            }
            if($log['server_type']==1){
                $province_count=Db::name('user')->where(['province'=>$log['province'],'server_type'=>array('eq',1)])->count();
                if($province_count>0){
                    $this->error('省代理已有存在');
                    $this->model->upDeposit($id,2);
                    $this->success("审核失败", null,0);
                }
            }
            if($log['server_type']==2){
                $city_count=Db::name('user')->where(['city'=>$log['city'],'server_type'=>array('eq',2)])->count();
                if($city_count>0){
                    $this->error('市代理已有存在');
                    $this->model->upDeposit($id,2);
                    $this->success("审核失败", null,0);
                }
            }
            if($log['server_type']==3){
                $area_count=Db::name('user')->where(['area'=>$log['area'],'server_type'=>array('eq',3)])->count();
                if($area_count>0){
                    $this->error('区代理已有存在');
                    $this->model->upDeposit($id,2);
                    $this->success("审核失败", null,0);
                }
            }

            if($log['server_type']==5){
                $area_count=Db::name('user')->where(['regione'=>$log['regione'],'server_type'=>array('eq',5)])->count();
                if($area_count>0){
                    $this->error('大区代理已有存在');
                    $this->model->upDeposit($id,2);
                    $this->success("审核失败", null,0);
                }
            }

            if($log['regione']>0){
                $user_datan['regione']=$log['regione'];
            }else{
                if($log['province']>0){
                    $area=new Area();
                    $areaOne=$area->where(['id'=>$log['province']])->find();
                    if(!empty($areaOne)){
                        $user_datan['regione']=$areaOne['regione_id'];
                    }else{
                        $user_datan['regione']=0;
                    }

                }else{
                    $user_datan['regione']=0;
                }

            }



            if($log['is_agent']>0){
                $userDatan['is_agent']=1;
//                $user_datan['province']=$log['province'];
//                $user_datan['city']=$log['province'];
//                $user_datan['area']=$log['area'];
                Db::name('user')->where(['id'=>$log['user_id']])->update($userDatan);
                $this->model->upDeposit($id,1);
                $UserSever=new User();
                $UserSever->VerifySetAgentId($log['user_id']);
                $AagentList=$UserSever->getShopAgentList($log['user_id']);

                if(!empty($AagentList)){
                    $AagentList=collection($AagentList)->toArray();
                }

                $site=\think\Config::get('site');
                $personalhigher=!empty($site['personalhigher'])?$site['personalhigher']:0;
                $personaluperior=!empty($site['personaluperior'])?$site['personaluperior']:0;
                $personal_level=!empty($site['personal_level'])?$site['personal_level']:0;

                foreach ($AagentList as $key=>$value){
                    if($value['level']==1){
                        $money=round($personal_level*($personalhigher/100),2);
                    }elseif($value['level']==2){
                        $money=round($personal_level*($personaluperior/100),2);
                    }
                    $UserOneList=\app\admin\model\User::get($value['id']);
                    $this->model->InsertAagentInverse($value['id'],$money);
                    $MoneyLog['user_id']=$value['id'];
                    $MoneyLog['money']=$money;
                    $MoneyLog['before']=round($UserOneList->money,2);
                    $MoneyLog['after']=round(($UserOneList->money+$money),2);;
                    $MoneyLog['createtime']=time();
                    $MoneyLog['memo']='开通个权';
                    $MoneyLog['money_type']='11';
                    $MoneyLog['shoporder_id']=$log['user_id'];
                    $this->model->InsertMoneyLog($MoneyLog);

                }


                $this->success("提交成功", null,1);
            }
            if($log['server_type']==1){
                $user_datan['province']=$log['province'];
                $user_datan['server_type']=$log['server_type'];
                Db::name('user')->where(['id'=>$log['user_id']])->update($user_datan);
                $this->model->upDeposit($id,1);
                $this->success("提交成功", null,1);

            }
            if($log['server_type']==2){
                $user_datan['province']=$log['province'];
                $user_datan['city']=$log['city'];
                $user_datan['server_type']=$log['server_type'];
                Db::name('user')->where(['id'=>$log['user_id']])->update($user_datan);
                $this->model->upDeposit($id,1);
                $this->success("提交成功", null,1);
            }
            if($log['server_type']==3){
                $user_datan['province']=$log['province'];
                $user_datan['city']=$log['city'];
                $user_datan['area']=$log['area'];
                $user_datan['server_type']=$log['server_type'];
                Db::name('user')->where(['id'=>$log['user_id']])->update($user_datan);
                $this->model->upDeposit($id,1);
                $this->success("提交成功", null,1);
            }

            if($log['server_type']==5){
                $user_datan['province']=$log['province'];
                $user_datan['city']=$log['city'];
                $user_datan['area']=$log['area'];
                $user_datan['server_type']=$log['server_type'];

                Db::name('user')->where(['id'=>$log['user_id']])->update($user_datan);
                $this->model->upDeposit($id,1);
                $this->success("提交成功", null,1);
            }
            $this->model->upDeposit($id,2);
            $this->success("审核失败", null,0);



        }


    }
}
